const Golbal = require("../../lib/js/global.js");
const SERVICE = require("../../lib/js/service.js");

Page({
  data:{
    inputValue: null,
    inputValue2: null,
    inputVal3: null,
    isActive: false,
    isActive2: false,
    isActive3: false,
    isViewPwd: false,
    mobileFocus: false,
    pwdFocus: false,
    vccodeFocus: false,
    text_or_graphics:0,    //选择登录方式 0 => 密码 1 => 验证码
    findPwd:false,
    vcNum: 0,   //验证码时间   
    mobile:null,    //手机号 
  },
  onLoad(){
 
  },
  
  /*分享*/ 
  onShareAppMessage(){
  },
  
  /*手机号*/
  Mobile(e){
    this.setData({
      inputVal:e.detail.value,
      isActive: Golbal.checkPhone(e.detail.value) === false ? false : true,
      mobile: e.detail.value,
    });
    
  },

  /*密码*/ 
  Pwd(e) {
    this.setData({
      inputVal2: e.detail.value,
      isActive2: Golbal.checkPwd(e.detail.value) === false ? false : true
    });

  },

  /*验证码*/
  Vccode(e) {
    this.setData({
      inputVal3: e.detail.value,
      isActive3: e.detail.value.length < 6 ? false : true
    });
  },

  /*清除手机号*/ 
  Clear(){
    this.setData({
      inputVal: null,
      isActive: false,
      mobileFocus:true,
      mobile:null
    });
  },

  /*清除密码*/ 
  Clear2() {
    this.setData({
      inputVal2: null,
      isActive2: false,
      pwdFocus: true
    });
  },

  /*清除验证码*/
  Clear3() {
    this.setData({
      inputVal3: null,
      isActive3: false,
      vccodeFocus: true
    });
  },

  /*查看密码*/ 
  viewPwd(){
    let td = this.data;
    this.setData({
      isViewPwd: !td.isViewPwd,
      // pwdFocus:true
    });    
  },
  
  /*忘记密码*/
  Ftpwd(){
    this.setData({
      findPwd:true
    });
    wx.setNavigationBarTitle({
      title: "忘记密码"//动态标题
    })
  },

  /*切换登录方式*/
  TextOrGraphics(e){
    let td = this.data;
    if(!td.findPwd){
      this.setData({
        text_or_graphics: td.text_or_graphics == 0 ? 1 : 0
      });
      wx.setNavigationBarTitle({
        title: td.text_or_graphics == 0 ? "密码登录" : "短信登录"
      })
    }else{
      this.setData({
        findPwd:false,
        text_or_graphics: td.text_or_graphics == 0 ? 1 : 0
      });
     
      wx.setNavigationBarTitle({
        title: td.text_or_graphics == 0 ? "密码登录" : "短信登录"
      })
    }
   
  },

  /*获取验证码*/
  getVc() {
    let _this = this, td = this.data;
    if (!td.mobile){
      wx.showToast({
        title: "请输入手机号",
        icon:"none",
        mask:true
      });
    } else if (td.mobile && Golbal.checkPhone(td.mobile) === false){
      wx.showToast({
        title: "请输入正确手机号",
        icon: "none",
        mask: true
      });
    } else{
      if (td.vcNum == 0){
        _this.setData({
          vcNum: 60
        });
        _this.VccodeTime();
        SERVICE.POST(SERVICE.URL.vccode, {
          phone: td.mobile,
        }, (status, res) => {
          if(!status){
            wx.showToast({
              title: res.data.message,
              mask: true
            });
          }
        });
      }
    }
  },

  /*验证码时间*/
  VccodeTime() {
    let _this = this, td = this.data;
    if (td.vcNum == 60) {
      let inter = setInterval(callback => {
        _this.setData({
          vcNum: --td.vcNum
        });
        if (td.vcNum == 0) {
          td.vcNum = 0;
          clearInterval(inter);
        }
      }, 1000);
    }
  },

  /*登录*/ 
  formSubmit(e){
    let _this = this, td = this.data, form_obj = e.detail.value;
    
    // if (e.detail.value.mobile && e.detail.value.pwd || e.detail.value.vccode.length == 6){
    //   if (td.findPwd){
    //     if (e.detail.value.vccode.length == 6){
         
    //    }
    //   }else{
    //     wx.showLoading({
    //       title: "登录中...",
    //       mask: true
    //     });
    //     SERVICE.POST(td.text_or_graphics == 0 ? SERVICE.URL.login : SERVICE.URL.vccodeLogin, {
    //       phone: e.detail.value.mobile,
    //       password: e.detail.value.pwd,
    //       code: e.detail.value.vccode
    //     }, (status, res) => {
    //       if (status) {
    //         wx.setStorageSync("thirdCookie", res.data.thirdCookie);
    //         _this.getUserInfo(res.data.thirdCookie);
    //         wx.showToast({
    //           title: res.data.message,
    //           icon: "none",
    //           mask: true,
    //           success() {
    //             setTimeout(() => {
    //               wx.reLaunch({
    //                 url: "../index/index",
    //               });
    //             }, 1500);
    //           }
    //         });

    //       } else {
    //         wx.showToast({
    //           title: res.data.message,
    //           icon: "none",
    //           mask: true
    //         });
    //       }
    //     });
    //   }
    // }

    if (td.findPwd){
      if (!form_obj.mobile) {
        wx.showToast({
          title: "请输入手机号",
          icon: "none",
          mask: true
        });
      } else if (form_obj.mobile && Golbal.checkPhone(form_obj.mobile) === false) {
        wx.showToast({
          title: "请输入正确手机号",
          icon: "none",
          mask: true
        });
      } else if (form_obj.pwd.length < 8){
        wx.showToast({
          title: "请输入完整的密码",
          icon: "none",
          mask: true
        });
      } else if (form_obj.vccode.length != 6){
        wx.showToast({
          title: "请输入完整的验证码",
          icon: "none",
          mask: true
        });
      }else{
        this.findPwd(form_obj);        
      }
    } else if (!td.findPwd){
      if (td.text_or_graphics == 0) {
        if (!form_obj.mobile) {
          wx.showToast({
            title: "请输入手机号",
            icon: "none",
            mask: true
          });
        } else if (form_obj.mobile && Golbal.checkPhone(form_obj.mobile) === false) {
          wx.showToast({
            title: "请输入正确手机号",
            icon: "none",
            mask: true
          });
        } else if (form_obj.pwd.length < 8) {
          wx.showToast({
            title: "请输入完整的密码",
            icon: "none",
            mask: true
          });
        } else {
          this.login(form_obj);
        }
      }else{
        if (!form_obj.mobile) {
          wx.showToast({
            title: "请输入手机号",
            icon: "none",
            mask: true
          });
        } else if (form_obj.mobile && Golbal.checkPhone(form_obj.mobile) === false) {
          wx.showToast({
            title: "请输入正确手机号",
            icon: "none",
            mask: true
          });
        } else if (form_obj.vccode.length < 6) {
          wx.showToast({
            title: "请输入验证码",
            icon: "none",
            mask: true
          });
        } else {
          this.vccodeLogin(form_obj);
        }
      }
      
    }
  },


  // 忘记密码
  findPwd(formObj){
    let _this = this;
    SERVICE.POST(SERVICE.URL.findPwd, {
      phone: formObj.mobile,
      password: formObj.pwd,
      code: formObj.vccode,
      findPassword: "findPassword"
    }, (status, res) => {
      if (status) {
        wx.setStorageSync("thirdCookie", res.data.thirdCookie);
        wx.showToast({
          title: res.data.message,
          icon: "none",
          mask: true,
          success() {
            wx.showToast({
              title: res.data.message,
              icon: "none",
              mask: true,
              success() {
                _this.setData({
                  findPwd: false
                });
              }
            });
          }
        });

      } else {
        wx.showToast({
          title: res.data.message,
          icon: "none",
          mask: true
        });
      }
    });
  },

  // 登录
  login(formObj){
    let _this = this;
    wx.showLoading({
      title: "登录中...",
      mask: true
    });
    SERVICE.POST(SERVICE.URL.login, {
      phone: formObj.mobile,
      password: formObj.pwd,
      code: formObj.vccode
    }, (status, res) => {
      if (status) {
        wx.setStorageSync("thirdCookie", res.data.thirdCookie);
        _this.getUserInfo(res.data.thirdCookie);
        wx.showToast({
          title: res.data.message,
          icon: "none",
          mask: true,
          success() {
            setTimeout(() => {
              wx.reLaunch({
                url: "../index/index",
              });
            }, 1500);
          }
        });

      } else {
        wx.showToast({
          title: res.data.message,
          icon: "none",
          mask: true
        });
      }
    });
  },

  // 短信登录
  vccodeLogin(formObj){
    let _this = this;
    wx.showLoading({
      title: "登录中...",
      mask: true
    });
    SERVICE.POST(SERVICE.URL.vccodeLogin, {
      phone: formObj.mobile,
      password: formObj.pwd,
      code: formObj.vccode
    }, (status, res) => {
      if (status) {
        wx.setStorageSync("thirdCookie", res.data.thirdCookie);
        _this.getUserInfo(res.data.thirdCookie);
        wx.showToast({
          title: res.data.message,
          icon: "none",
          mask: true,
          success() {
            setTimeout(() => {
              wx.reLaunch({
                url: "../index/index",
              });
            }, 1500);
          }
        });

      } else {
        wx.showToast({
          title: res.data.message,
          icon: "none",
          mask: true
        });
      }
    });
  },

  /*获取用户信息*/ 
  getUserInfo(thirdCookie){
    SERVICE.POST(SERVICE.URL.userInfo,{
      thirdCookie: thirdCookie
    },(status,res)=>{
      if(res.data.status == "success"){
        wx.setStorageSync("userInfo", res.data.user_info);
      }else{
        wx.showToast({
          title: "获取信息失败",
          icon: "none",
          mask:true
        });
      }
    });
  }
});